Imports System.Data

Partial Class WebForms_WebUserControl
    Inherits WebFormCommon
    Dim dt As DataTable

    'TODO: Filter based on Status (for new business/client servicing)
    Dim strPath As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim sec As New Secure

        sec.ParentPage = ParentPage
        sec.AddControl(AddNewBusiness, Secure.ControlTypes.AddType)
        sec.AddControl(Print_btn, Secure.ControlTypes.PrintType)
        sec.AddControl(Save_btn, Secure.ControlTypes.PrintType)
        sec.AddDataGridColumn(DataGrid, 6, Secure.ControlTypes.EditType)
        sec.AddDataGridColumn(DataGrid, 7, Secure.ControlTypes.DeleteType)

        sec.SetPageAccess(Session("UserName"))

        Select Case PageControlMode
            Case ControlMode.NewBusiness
                strPath = "New_Business"
                Refferal_lbl.Visible = False
                Search_Referrals.Visible = False
            Case ControlMode.ClientServicing
                strPath = "Client_Servicing"
                AddNewBusiness.Visible = False
        End Select

        If Not Page.IsPostBack Then
            Call Search()
        End If

        sec = Nothing

    End Sub


    Protected Sub Search_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Search_btn.Click
        Call Search()
    End Sub

    Private Sub Search()
        Dim dm As New DAOMain.VW_POLICY_TRANS_SEARCH_DAO
        Dim vo As New DAOMain.VW_POLICY_TRANS_SEARCH_VO

        With vo
            .POLICY_CLIENT_LAST_NAME = "%" & Me.Lastname_txt.Text & "%"
            .POLICY_CLIENT_FIRST_NAME = "%" & Me.Firstname_txt.Text & "%"
            .POLICY_CLIENT_MIDDLE_NAME = "%" & Me.Middlename_txt.Text & "%"
            .POLICY_CLIENT_ALIAS = "%" & Me.Alias_txt.Text & "%"
            .POLICY_NO = "%" & Me.Policynumber_txt.Text & "%"
            .PROVIDER_DESC = "%" & Me.Provider_txt.Text & "%"
            .PRDT_DESC = "%" & Me.Product_txt.Text & "%"

            If Not IsNothing(Session("advisor")) Then
                .ADVISOR = Session("UserName") & "%"
            End If

            Select Case Me.PageControlMode
                Case ControlMode.ClientServicing
                    .PROCESS_ST = "%CS%"
                Case ControlMode.NewBusiness
                    .PROCESS_ST = "%NB%"
            End Select
        End With

        dt = dm.Search(vo, , True)

        results_label.Text = dt.Rows.Count

        ViewState.Add("dtable", dt)
        DataGrid.CurrentPageIndex = 0
        DataGrid.DataSource = ViewState("dtable")
        DataGrid.DataBind()

        dm = Nothing
    End Sub

    Protected Sub DataGrid_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid.ItemCommand
        Select Case e.CommandName

            Case "Edit"
                Session.Add("Seq_CD", e.Item.Cells(0).Text)
                Session.Add("PolicyNo", e.Item.Cells(2).Text)
                Session.Add("Provider", e.Item.Cells(3).Text & " - " & e.Item.Cells(4).Text)
                Session.Add("mode", "edit")

                Response.Redirect("~/" & strPath & "/ClientInfo.aspx")

            Case "View"
                Session.Add("Seq_CD", e.Item.Cells(0).Text)
                Session.Add("PolicyNo", e.Item.Cells(2).Text)
                Session.Add("Provider", e.Item.Cells(3).Text & " - " & e.Item.Cells(4).Text)
                Session.Add("mode", "view")

                Response.Redirect("~/" & strPath & "/ClientInfo.aspx")

            Case "Delete"
                Dim dao As New DAOMain.POLICY_TRANS_DAO
                Dim vo As New DAOMain.POLICY_TRANS_VO

                vo.POLICY_SEQ_CD = e.Item.Cells(0).Text

                dao.AddParameter(vo.POLICY_SEQ_CD)
                Dim dt As DataTable = dao.ExecQryProc("sp_get_record_count")

                If dt.Rows.Count > 0 Then
                    If dt.Rows(0)("RecordCount") > 0 Then
                        Dim polyFunc As New PolyFunc.ASPFunc
                        polyFunc.MessageAlert(ParentPage, "Cannot Delete Record! There are existing details!")
                        polyFunc = Nothing
                    Else
                        dao.Delete(vo)
                        Call Search()
                    End If
                End If

                dao = Nothing
                vo = Nothing
        End Select
    End Sub

    Protected Sub Save_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Save_btn.Click
        Dim crpt As New CReports
        'Dim dao As New DAOMain.VW_POLICY_TRANS_SEARCH_DAO
        'Dim vo As New DAOMain.VW_POLICY_TRANS_SEARCH_VO

        'With vo
        '    .POLICY_CLIENT_LAST_NAME = "%" & Lastname_txt.Text & "%"
        '    .POLICY_CLIENT_FIRST_NAME = "%" & Firstname_txt.Text & "%"
        '    .POLICY_CLIENT_MIDDLE_NAME = "%" & Middlename_txt.Text & "%"
        '    .POLICY_CLIENT_ALIAS = "%" & Alias_txt.Text & "%"
        '    .POLICY_NO = "%" & Policynumber_txt.Text & "%"
        '    .PROVIDER_DESC = "%" & Provider_txt.Text & "%"
        '    .PRDT_DESC = "%" & Product_txt.Text & "%"
        'End With

        With crpt
            'Dim dt As DataTable = dao.Search(vo, , True)
            If Not IsNothing(ViewState("dtable")) Then
                Dim dt As DataTable = ViewState("dtable")

                .ShowRpt("PolicySearchExport", CReports.RptType.Excel_Report, ParentPage, dt, "vw_policy_trans_search")
            End If
        End With

        'dao = Nothing
        'vo = Nothing
        crpt = Nothing
    End Sub

    Protected Sub Search_ClientIssues_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Search_ClientIssues.Click
        Response.Redirect("~/" & strPath & "/Search_CIssues.aspx")
    End Sub

    Protected Sub Search_Referrals_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Search_Referrals.Click
        Response.Redirect("~/" & strPath & "/Search_Referrals.aspx")
    End Sub

    Protected Sub DataGrid_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid.PageIndexChanged
        If Not IsNothing(ViewState("dtable")) Then
            If DataGrid.PageCount >= e.NewPageIndex Then
                DataGrid.CurrentPageIndex = e.NewPageIndex
                DataGrid.DataSource = ViewState("dtable")
                DataGrid.DataBind()
            End If
        End If
    End Sub
End Class
